package Monitors;

/******************************************************************************************************************
 * File: ServiceMaintenanceConsole.java
 * Course: 17655
 * Project: Assignment 3
 * Copyright: Copyright (c) 2009 Carnegie Mellon University
 * Versions:
 *	Match 2011 
 *           Base
 *
 * Parameters: None
 *
 * Internal Methods: None
 *
 ******************************************************************************************************************/
import TermioPackage.*;
import EventPackage.*;
import util.FileConfigReader;
import util.FileReadingException;
import util.InvalidFileFormatException;

public class ServiceMaintenanceConsole {

    private static IsAliveMonitor Monitor = null;	// The service monitor
    // maximum time an event is kept for retry
    private static int time_to_live;
    // time between intervals
    private static int retry_interval;
    // warning time
    public static int warning_time;
    // error time
    public static int error_time;

    public static void main(String args[]) {
        Termio UserInput = new Termio();	// Termio IO Object
        boolean Done = false;			// Main loop flag
        String Option = null;                   // Menu choice from user
        Event Evt = null;			// Event object
        boolean Error = false;			// Error flag

        if (args.length > 0) {

            // Read settings from configuration file
            FileConfigReader config;
            try {
                config = new FileConfigReader(args[0]);

                retry_interval = config.getRetryInterval();
                time_to_live = config.getTtl();
                warning_time = config.getWarningTime();
                error_time = config.getErrorTime();

            } catch (FileReadingException e1) {
                System.out.println("Error reading configuration file:" + e1.getMessage());
                e1.printStackTrace();
                System.exit(0);
            } catch (InvalidFileFormatException e1) {
                System.out.println("Error reading configuration file:" + e1.getMessage());
                e1.printStackTrace();
                System.exit(0);
            }
        } else {
            System.out.println("No configuration file provided. Please provide it as the first argument!");
            System.exit(0);
        }

        
        /////////////////////////////////////////////////////////////////////////////////
        // Get the IP address of the event manager
        /////////////////////////////////////////////////////////////////////////////////
        if (args.length > 1) {
            // event manager is not on the local system

            Monitor = new IsAliveMonitor(args[1]);

        } else {

            Monitor = new IsAliveMonitor();

        } // if

        // Pass retry API settings to monitor
        Monitor.setTime_to_live(time_to_live);

        Monitor.setRetry_interval(retry_interval);
        // Here we check to see if registration worked. If ef is null then the
        // event manager interface was not properly created.
        if (Monitor.IsRegistered()) {

            Monitor.start(); // Here we start the monitoring and control thread

            while (!Done) {
                // Here, the main thread continues and provides the main menu

                System.out.println("\n\n\n\n");
                System.out.println("Service Maintenance Console: \n");

                if (args.length != 0) {
                    System.out.println("Using event manager at: " + args[0] + "\n");
                } else {
                    System.out.println("Using local event manager \n");
                }


                System.out.println("Select an Option: \n");

                System.out.println("X: Stop System\n");
                System.out.print("\n>>>> ");
                Option = UserInput.KeyboardReadString();


                //////////// option X ////////////

                if (Option.equalsIgnoreCase("X")) {
                    // Here the user is done, so we set the Done flag and halt
                    // the environmental control system. The monitor provides a method
                    // to do this. Its important to have processes release their queues
                    // with the event manager. If these queues are not released these
                    // become dead queues and they collect events and will eventually
                    // cause problems for the event manager.

                    Monitor.Halt();
                    Done = true;
                    System.out.println("\nConsole Stopped... Exit monitor mindow to return to command prompt.");
                    Monitor.Halt();

                } // if

            } // while

        } else {

            System.out.println("\n\nUnable start the monitor.\n\n");

        } // if
    } // main
} // SecurityFireConsole

